package com.Example;

import javax.xml.bind.annotation.XmlElementDecl.GLOBAL;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class GameLoop extends Screen {
	
	Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();	
	TextureWrapper backTexture;
	TextureWrapper[] layers;
	FPSLogger fps;
	BoxObjectManager boxManager;
	
	BoxRectObject ground;
	BoxCircleObject ball;
	BoxPigeon boxPigeon;
	ParallaxCamera cam;
	private BoxMap tiledMap;
	
	public GameLoop(int screenId,ParallaxCamera cam){
		fps=new FPSLogger();
		this.BackScreenID=screenId;
		spritebatch=new SpriteBatch();
		spritebatch1=new SpriteBatch();
		
		spritebatch.setProjectionMatrix(cam.combined);
		spritebatch1.setProjectionMatrix(cam.combined);
		
		IsDone=false;
		TouchPoint=new Vector3(0,0,0);
		TouchHandler=new TouchHandler();
		this.cam=cam;
		Init();
	}
	
	private void Init() {
		// TODO Auto-generated method stub
		Assets.Load();
		
//		backTexture=new TextureWrapper(Assets.backRegion,new Vector2(GlobalSettings.VIRTUAL_WIDTH/2,
//				GlobalSettings.VIRTUAL_HEIGHT/2));
//		backTexture.SetDimension(TextureDimensions.BACK_WIDTH, TextureDimensions.BACK_HEIGHT);
		
		boxManager=new BoxObjectManager();
		
		//InitGround();
		InitBackground();
		
		
		InitMap();

		InitTurrets();
		
		InitBall();
	}
	
	

	private void InitTurrets() {
		Turret turret = new Turret(boxManager.GetNewObjectIndex(), GlobalSettings.ENEMY_GROUP, 700, 150, 0,cam.position);
		boxManager.AddObject(turret);
//		Bullet turret = new Bullet(boxManager.GetNewObjectIndex(), GlobalSettings.ENEMY_GROUP, 100, 190, 0);
		//turret.SetTextureDimension(TextureDimensions.BALL_WIDTH, TextureDimensions.BALL_HEIGHT);
//		boxManager.AddObject(turret);
	}

	private void InitMap() {
		tiledMap= new BoxMap("data/world/level1/level.tmx", "data/packer", "data/collisions.txt", cam);
		
	}
	
	private void InitBackground(){
		this.layers = new TextureWrapper[3];
		this.layers[0] = new TextureWrapper(Assets.sky, new Vector2(0,0));
		this.layers[0].SetDimension(GlobalSettings.VIRTUAL_WIDTH, GlobalSettings.VIRTUAL_HEIGHT);
		
		this.layers[1] = new TextureWrapper(Assets.mountains, new Vector2(0,0));
		this.layers[1].SetDimension(GlobalSettings.VIRTUAL_WIDTH, GlobalSettings.VIRTUAL_HEIGHT);
		
	}

	private void InitBall() {
		// TODO Auto-generated method stub
		int gRadius=TextureDimensions.BALL_WIDTH/2;
		int gPositionX=160;
		int gPositionY=280;
		ball=new BoxCircleObject(boxManager.GetNewObjectIndex(), GlobalSettings.ENEMY_GROUP,
				gRadius, BodyType.DynamicBody,
				1, 1, gPositionX, gPositionY, 0, Assets.ballRegion);
		ball.SetTextureDimension(TextureDimensions.BALL_WIDTH, TextureDimensions.BALL_HEIGHT);
		boxManager.AddObject(ball);
		
		boxPigeon=new BoxPigeon(boxManager.GetNewObjectIndex(), GlobalSettings.PIGEON_GROUP,
				BodyType.DynamicBody,
				 gPositionX-50, gPositionY, 05f);
		boxPigeon.SetTextureDimension(TextureDimensions.BALL_WIDTH, TextureDimensions.BALL_HEIGHT);
		boxManager.AddObject(boxPigeon);
	}

	private void InitGround() {
		// TODO Auto-generated method stub
		int gWidth=TextureDimensions.GROUND_WIDTH;
		int gHeight=TextureDimensions.GROUND_HEIGHT;
		int gPositionX=160;
		int gPositionY=40;
		ground=new BoxRectObject(boxManager.GetNewObjectIndex(), GlobalSettings.GROUND_GROUP,
				gWidth, gHeight, BodyType.StaticBody,
				0, 0, gPositionX, gPositionY, 0, Assets.groundRegion);
		ground.SetTextureDimension(gWidth, gHeight);
		boxManager.AddObject(ground);
		
	}

	@Override
	public void update(float dt) {
		// TODO Auto-generated method stub
		
		//boxPigeon.IsAlive=false;
		boxManager.Update(dt);
	}

	@Override
	public void render() {
		Gdx.graphics.getGL10().glClear(GL10.GL_COLOR_BUFFER_BIT);
		//cam.update();
		
		
		//spritebatch1.setProjectionMatrix(cam.combined);
		spritebatch1.setProjectionMatrix(cam.calculateParallaxMatrix(0, 0));
		spritebatch1.disableBlending();
		spritebatch1.begin();
		this.layers[0].Draw(spritebatch1);
		spritebatch1.end();
		
		spritebatch1.setProjectionMatrix(cam.calculateParallaxMatrix(0.5f, 1));
		spritebatch1.enableBlending();
		spritebatch1.begin();
		this.layers[1].Draw(spritebatch1);
		spritebatch1.end();
		
		tiledMap.render();
		
		spritebatch.setProjectionMatrix(cam.calculateParallaxMatrix(1f, 1));
		spritebatch.begin();
		spritebatch.enableBlending();
		boxManager.Draw(spritebatch);
		spritebatch.end();
		
		
		debugRenderer.render(BoxObjectManager.GetWorld(), spritebatch.getProjectionMatrix() );
		fps.log();
		
	}

	@Override
	public boolean isDone() {
		// TODO Auto-generated method stub
		return IsDone;
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		Assets.Dispose();
		boxManager.Dispose();
	}

	@Override
	public void OnPause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void OnResume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void OnKeyUp(int keyCode) {
		// TODO Auto-generated method stub

	}

	@Override
	public void OnKeyDown(int keyCode) {
		// TODO Auto-generated method stub

	}

}
